#include <assert.h>
#include <stdio.h>
#include "base64.h"
void print_ustr_hex(const unsigned char *str, int len) {
    printf("\n");
    for (int i = 0;i < len; i++) {
        if ( len && (len % 8 == 0)) {
            printf("\n");
        }
        printf("%02x ", str[ i ]);
    }
    printf("\n");
}

void test_base64_encode( const unsigned char *s, int len, const char *exp) {
    char *cipher = base64_encode( s, len, NULL);

    if ( strcmp( cipher, exp) ) {
        printf("base64_encode(\"%s\") failed!\n", s);
        printf("expected: %s\n", exp);
        printf("real: %s\n", cipher);
    }
    else {
        printf("base64_encode(\"%s\") success\n", s);
    }

    free(cipher);
}
void test_base64_decode( const char *cipher, int plen, const unsigned char *exp) {
    int rlen;

    const unsigned char * plain = base64_decode( cipher, &rlen, NULL);

    if ( plen != rlen || memcmp( plain, exp, plen * sizeof(unsigned char)) ) {
        printf("base64_decode(\"%s\") failed!\n", cipher);

        printf("expected plain len: %d, real plain len: %d\n", plen, rlen);

        printf("expected plain str in hex:\n");
        print_ustr_hex( exp, plen);
        printf("real plain str in hex: \n");
        print_ustr_hex( plain, rlen);
    }
    else {
        printf("base64_decode(\"%s\") success!\n", cipher);
    }

    free( plain );
}

void test_encode_hello() {
    test_base64_encode("hello,world", 11, "aGVsbG8sd29ybGQ=");
}
void test_encode_life() {
    test_base64_encode("life is a fucking movie!", 24, "bGlmZSBpcyBhIGZ1Y2tpbmcgbW92aWUh");    
}
void test_encode_empty() {
    test_base64_encode("", 0, "");
}
void test_encode_a() {
    test_base64_encode("a", 1, "YQ==");
}
void test_encode_12() {
    test_base64_encode("12", 2, "MTI=");
}
void test_encode_china() {
    test_base64_encode("中国", 6, "5Lit5Zu9");
}


void test_decode_poem() {
    const char *poem = "To see a world in a grain of sand,";
    int rlen;

    test_base64_decode("VG8gc2VlIGEgd29ybGQgaW4gYSBncmFpbiBvZiBzYW5kLA==", 34, poem);
}
void test_decode_empty() {
    test_base64_decode("", 0, "");
}

void test_decode_chinadaily() {
    const char *plain = "As this year marks the 50th anniversary of the China-Bangladesh relationship, Yunus said the two neighboring countries have set an example of state-to-state relations and people-to-people exchanges that the rest of the world could follow.";

    test_base64_decode("QXMgdGhpcyB5ZWFyIG1hcmtzIHRoZSA1MHRoIGFubml2ZXJzYXJ5IG9mIHRoZSBDaGluYS1CYW5nbGFkZXNoIHJlbGF0aW9uc2hpcCwgWXVudXMgc2FpZCB0aGUgdHdvIG5laWdoYm9yaW5nIGNvdW50cmllcyBoYXZlIHNldCBhbiBleGFtcGxlIG9mIHN0YXRlLXRvLXN0YXRlIHJlbGF0aW9ucyBhbmQgcGVvcGxlLXRvLXBlb3BsZSBleGNoYW5nZXMgdGhhdCB0aGUgcmVzdCBvZiB0aGUgd29ybGQgY291bGQgZm9sbG93Lg==", 238, plain);
}

int main()
{

    test_encode_hello();
    test_encode_life();
    test_encode_empty();
    test_encode_a();
    test_encode_12();
    test_encode_china();

    printf("\n");
    test_decode_poem();
    test_decode_empty();
    test_decode_chinadaily();
    return 0;
}